//
//  LoginController.swift
//  BodyBuilding
//
//  Created by franze on 2017/10/6.
//  Copyright © 2017年 franze. All rights reserved.
//

import UIKit

class LoginController: UIViewController {
    var accountField:UITextField!
    var pwdField:UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        viewConfiguration()
    }
    
    func viewConfiguration(){
        view.backgroundColor = UIColor.white
        navigationController?.isNavigationBarHidden = true
        
        let label = UILabel(frame: CGRect(origin: .zero, size: CGSize(width: 100, height: 40)))
        label.font = UIFont.boldSystemFont(ofSize: label.bounds.height)
        label.text = "欢迎"
        label.center = CGPoint(x: view.center.x, y: 160)
        label.textColor = UIColor(red: 18/255, green: 150/255, blue: 219/255, alpha: 1)
        label.textAlignment = .center
        view.addSubview(label)
        
        let avatarView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 38, height: 34)))
        let avatar = UIImageView(frame: CGRect(x: 0, y: 0, width: 34, height: 34))
        avatar.image = UIImage(named: "login_avatar")?.scaleImageToSize(avatar.bounds.size)
        avatarView.addSubview(avatar)
        
        accountField = UITextField(frame: CGRect(x: 50, y: 240, width: WIDTH-100, height: 34))
        accountField.font = UIFont.systemFont(ofSize: 16)
        accountField.leftViewMode = .always
        accountField.leftView = avatarView
        view.addSubview(accountField)
        
        let lockView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 38, height: 34)))
        let lock = UIImageView(frame: CGRect(x: 2, y: 0, width: 30, height: 30))
        lock.image = UIImage(named: "lock")?.scaleImageToSize(lock.bounds.size)
        lockView.addSubview(lock)
        
        pwdField = UITextField(frame: CGRect(x: 50, y: 300, width: WIDTH-100, height: 34))
        pwdField.font = UIFont.systemFont(ofSize: 16)
        pwdField.isSecureTextEntry = true
        pwdField.leftViewMode = .always
        pwdField.leftView = lockView
        view.addSubview(pwdField)
        
        let line = CAShapeLayer()
        line.frame = CGRect(x: 0, y: accountField.bounds.height+3, width: accountField.bounds.width, height: 1)
        line.backgroundColor = UIColor.lightGray.cgColor
        accountField.layer.addSublayer(line)
        
        let sline = CAShapeLayer()
        sline.frame = CGRect(x: 0, y: accountField.bounds.height+3, width: accountField.bounds.width, height: 1)
        sline.backgroundColor = UIColor.lightGray.cgColor
        pwdField.layer.addSublayer(sline)
        
        let loginBtn = CustomButton(frame: CGRect(origin: .zero, size: CGSize(width: 140, height: 44)))
        loginBtn.center = CGPoint(x: view.center.x, y: 430)
        loginBtn.setTitle("登录", for: .normal)
        loginBtn.backgroundColor = UIColor(red: 18/255, green: 150/255, blue: 219/255, alpha: 1)
        loginBtn.layer.cornerRadius = 8
        loginBtn.layer.masksToBounds = true
        view.addSubview(loginBtn)
        
        let registerBtn = UIButton(frame: CGRect(origin: .zero, size: CGSize(width: 40, height: 40)))
        registerBtn.center = CGPoint(x: view.center.x, y: HEIGHT-30)
        registerBtn.setTitle("注册", for: .normal)
        registerBtn.setTitleColor(.lightGray, for: .normal)
        registerBtn.titleLabel?.font = UIFont.systemFont(ofSize: 18)
        view.addSubview(registerBtn)
        
        loginBtn.addTarget(self, action: #selector(login), for: .touchUpInside)
        registerBtn.addTarget(self, action: #selector(register), for: .touchUpInside)
    }
    
    @objc func login(sender:CustomButton){
        sender.isEnabled = false
        sender.animation()
        let parameter:NSDictionary = ["tel":accountField.text!,"password":pwdField.text!]
        DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
            Networking.shared.login(parameter: parameter) { (success) in
                if success{
                    UIApplication.shared.keyWindow?.rootViewController = MasterController()
                    sender.stopAnimation()
                }else{
                    sender.stopAnimation()
                }
                sender.isEnabled = true
            }
        }
    }
    
    @objc func register(){
        _ = navigationController?.pushViewController(RegisterController(), animated: true)
    }
        
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
